﻿using IFRS.DataAccess.DAL;
using IFRS.Models;
using IFRS.Web;
using IFRS_Global;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Threading;
using System.Web.UI;

public partial class IFRS_WIN13_2E : PageBase
{
    #region "---------- Events -----------"
    protected void Page_Load(object sender, EventArgs e)
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        if (!(Page.IsPostBack || Page.IsCallback))
        {
            this.AddClientScript();
            this.ViewMenu();
            this.BindDDL();
            this.SetErrorMessage();
            if (Request.QueryString["id"] != null)
            {
                this.BindData(Convert.ToInt32(Request.QueryString["id"].ToString()));
            }          
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }

    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData();
    }

    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["IsRef"] != null)
            Response.Redirect("~/IFRS/WIN25_1S.aspx");
        else
            Response.Redirect("~/IFRS/WIN13_1S.aspx");
    }
    #endregion "---------- Events -----------"

    #region "---------- Methods -----------"

    private void AddClientScript()
    {
        btnCANCEL.Attributes["OnClick"] = "if(!confirm(\"" + Resources.ifrsResource.ConfirmCancel + "\")) return false;";
    }

    private void ViewMenu()
    {
        MenuTitleBar = Resources.ifrsMenu.WIN13_2E;
    }

    private void SetErrorMessage()
    {
        btnSAVE.ValidationGroup = "Master";
        reqDate_Transection.SetRequiredField(lblDate_Transection, txtDate_Transection, btnSAVE);
        reqSourceData.SetRequiredField(lblSourceData, ddlSourceData, btnSAVE);
        reqAccountNo_ContractNumber.SetRequiredField(lblAccountNo_ContractNumber, txtAccountNo_ContractNumber, btnSAVE);
        reqDate_Payment.SetRequiredField(lblDate_Payment, txtDate_Payment, btnSAVE);
        reqIndividualImpairAmount.SetRequiredField(lblIndividualImpairAmount, txtIndividualImpairAmount, btnSAVE);
        reqCollateralAmount.SetRequiredField(lblCollateralAmount, txtCollateralAmount, btnSAVE);
        reqInterestRate.SetRequiredField(lblInterestRate, txtInterestRate, btnSAVE);
    }

    private void BindData(int ID)
    {
        List<IndividualImpair> individualImpair = new List<IndividualImpair>();
        DataTable dt = new SPR_INDIVIDUALIMPAIR().SEARCH_BYID(ID);
        if (dt.Rows.Count > 0)
        {
            ConvertHelper.ConvertDataTableToObject(dt, individualImpair);
            txtDate_Transection.Text = individualImpair[0].TransactionDate.DateToString();
            ddlSourceData.SelectedValue = individualImpair[0].SourceDataID.ToString();
            txtAccountNo_ContractNumber.Text = individualImpair[0].AccountNo;
            txtDate_Payment.Text = individualImpair[0].PaymentDate.DateToString();
            txtIndividualImpairAmount.Text = individualImpair[0].IndividualImpairAmount.ToString();
            txtCollateralAmount.Text = individualImpair[0].CollateralAmount.ToString();
            txtInterestRate.Text = individualImpair[0].InterestRate.ToString();
        }
    }

    private void SaveData()
    {
        try
        {
            IndividualImpair individualImpair = new IndividualImpair();
            individualImpair.IndividualImpairID = Convert.ToInt32(Request.QueryString["id"].ToString());
            individualImpair.TransactionDate = txtDate_Transection.Text.StringToDateAllowNull();
            individualImpair.SourceDataID = ddlSourceData.SelectedValue.AsInt32();
            individualImpair.AccountNo = txtAccountNo_ContractNumber.Text;
            individualImpair.PaymentDate = txtDate_Payment.Text.StringToDateAllowNull();
            individualImpair.IndividualImpairAmount = Convert.ToDecimal(txtIndividualImpairAmount.Text);
            individualImpair.CollateralAmount = Convert.ToDecimal(txtCollateralAmount.Text);
            individualImpair.InterestRate = Convert.ToDecimal(txtInterestRate.Text);
            individualImpair.IsConfirmed = false;
            individualImpair.SetStandardField();
            //individualImpair.CreatedDate = DateTime.Now;
            //individualImpair.CreatedBy = SessionManager.IFRS_EMPLOYEE_ID;
            //individualImpair.CreatedCostCenter = SessionManager.IFRS_COST_CENTER;
            //individualImpair.CreatedBranch = SessionManager.IFRS_BRANCH;
            //individualImpair.UpdatedDate = DateTime.Now;
            //individualImpair.UpdatedBy = SessionManager.IFRS_EMPLOYEE_ID;
            //individualImpair.UpdatedCostCenter = SessionManager.IFRS_COST_CENTER;
            //individualImpair.UpdatedBranch = SessionManager.IFRS_BRANCH;

            int result = new SPR_INDIVIDUALIMPAIR().UPDATE(individualImpair);

            if (result > 0)
            {
                if (Request.QueryString["IsRef"] != null)
                    this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN13_2E.aspx?&id={0}&IsRef={1}", individualImpair.IndividualImpairID, Request.QueryString["IsRef"].ToString())));
                else
                    this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN13_2E.aspx?&id={0}", individualImpair.IndividualImpairID)));
            }
            else
            {
                if (result == -1)
                {
                    Utility.AlertMessage(Page, Resources.ifrsResource.Duplicate);
                }
                else
                {
                    Utility.AlertMessage(Page, Resources.ifrsResource.CanNotSaveData);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void BindDDL()
    {
        ddlSourceData.SetData(DDL_TYPE.SourceData, "", false, true);
    }

    #endregion ""---------- Methods -----------"
}